﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;
using System.Web.Security;

namespace TeamFranceTriple
{
    public class PollItemEventArgs : EventArgs
    {
        public Int32 PollId { get; private set; }
        public PollItemEventArgs(Int32 PollId)
        {
            this.PollId = PollId;
        }
    }

    public partial class ShowOnePoll : BaseUserControl
    {
        protected Boolean CanVote { get; set; }
        public String UserIpAddress { get; set; }

        public event EventHandler<PollItemEventArgs> EditPollButtonClick;

        public Int32 PollId
        {
            get { return Int32.Parse(PollEntityDataSource.WhereParameters["PollIdParam"].DefaultValue); }
            set
            {
                PollEntityDataSource.WhereParameters["PollIdParam"].DefaultValue = value.ToString();
                AnswerEntityDataSource.WhereParameters["PollIdParam"].DefaultValue = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ThisMaster.Requester.Membership == null)
            {
                this.CanVote = false;
                return;
            }

            if (ThisMaster.Requester.IsAuthenticated)
                this.CanVote = PollDal.CanVote(this.PollId, Guid.Parse(ThisMaster.Requester.Membership.ProviderUserKey.ToString()));
            else
                this.CanVote = false;

            if (ThisMaster.Requester.Profile.UserName.ToLower() == ForumTopicDal.GetEntity(ForumTopicDal.GetTopicIdByPollId(this.PollId)).AuthorForumTopic.ToLower())
            {
                EditPollButton.Visible = true;
                DeletePollButton.Visible = true;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (PollDal.GetEntity(PollId) == null)
            {
                ShowOnePollPanel.Visible = false;
            }
        }

        protected void AnswerListView_SelectedIndexChanged(Object sender, EventArgs e)
        {
            if (!this.CanVote) return;
            //Enregistre le fait qu'un utilisateur a voté
            PollDal.PollAndCreatePoolUser(Int32.Parse(AnswerListView.SelectedValue.ToString()), new PollUser()
                {
                    UserId = Guid.Parse(ThisMaster.Requester.Membership.ProviderUserKey.ToString()),
                    UserIp = this.UserIpAddress
                });
            this.CanVote = false;
        }

        protected void AnswerListView_OnItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Button btnSelect = e.Item.FindControl("SelectButton") as Button;
                Label pollResult = (Label)e.Item.FindControl("NumberVotePollAnswerLabel");

                if (!this.CanVote)
                {
                    if (btnSelect != null)
                        btnSelect.Visible = false;
                    if (pollResult != null)
                        pollResult.Visible = true;
                }
                else
                {
                    if (btnSelect != null)
                        btnSelect.Visible = true;
                    if (pollResult != null)
                        pollResult.Visible = false;
                }
            }
        }

        protected void EditPollButton_Click(object sender, EventArgs e)
        {
            if (EditPollButtonClick != null)
                EditPollButtonClick(this, new PollItemEventArgs(this.PollId));
        }

        protected void DeletePollButton_Click(object sender, EventArgs e)
        {
            PollDal.DeleteEntity(PollId);
        }
    }
}